package project.active;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import project.adapter.Projectlist_adapter_tc;
import project.dao.PrProjectDAO;
import project.model.PrProject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import approval.dao.LangDAO;
import approve.action.LoginActivity;
import approve.action.R;
import model.pojo.Employee;
import model.pojo.Languages;

public class ListProjectManager extends Activity{
	ListView lstProjectManager;
	private String lang="EN",database="admin";
	Employee emp=new Employee();
	TextView txtListManager;
	private boolean read = true;
	private Bundle alllangs = new Bundle();
	TextView logout,username;
	Bundle language;
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.projectlistmanager);
			lstProjectManager = (ListView)findViewById(R.id.listprojectmanager);
//			initAttribute();
			TextView titlePr = (TextView)findViewById(R.id.titleProjectManager);
			
			
			Intent in = this.getIntent();
			emp = (Employee)in.getSerializableExtra("em");
			
			language = getIntent().getExtras().getBundle(getLang());
			logout=(TextView)findViewById(R.id.logout);
			username=(TextView)findViewById(R.id.username);
			logout.setText(language.getString("Logout"));
			
			logout.setOnClickListener(listenerLogout);
			username.setText(emp.getUserName());

//			report=(TextView)findViewById(R.id.report);report.setText("22");
			titlePr.setText(language.getString("ac_SUProjectList") + "-" + language.getString("ac_Manager"));
			
			Projectlist_adapter_tc adapter1=new Projectlist_adapter_tc(this, loadListProjectManager(emp.getIdemployee()),language.getString("ac_lien_STu"),language.getString("ac_lien_SDen"));
//			lstProjectManager.setAdapter(new ArrayAdapter<PrProject>(this, android.R.layout.simple_list_item_1,loadListProjectManager(emp.getIdemployee(),getDatabase())));
			lstProjectManager.setAdapter(adapter1);
			lstProjectManager.setOnItemClickListener(detailProjectManager);
			
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(ListProjectManager.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	public String getLang(){
    	Bundle extras = getIntent().getExtras();
    	if(extras != null){
    		lang = extras.getString("lang");
    	}
    	return lang;
    }
    
 
    public List<PrProject> loadListProjectManager(String idEmp) throws SQLException{
//    	List<PrProject> lst = new ArrayList<PrProject>();
    	List<PrProject> lst = PrProjectDAO.getAllProjectManager(emp.getIdemployee(),getDatabase());
    	if(lst.size()==0){
			TextView  nodata = new TextView(this);
    		nodata.setText(language.getString("nodata"));
    		setContentView(nodata);
    	}else{
    		return lst;
    	}
//    	lst = PrProjectDAO.getAllProjectManager(emp.getIdemployee(),getDatabase());
    	
    	
    	return lst;
    }

    public OnItemClickListener detailProjectManager = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
//			String item = arg0.getItemAtPosition(arg2).toString();
			Intent intent = new Intent(ListProjectManager.this,ProjectManagerDetail.class);
			PrProject project = (PrProject)lstProjectManager.getItemAtPosition(arg2);
			intent.putExtras(getIntent().getExtras());
			intent.putExtra("project",project);
			intent.putExtra("emp", emp);
			intent.putExtra("databaseMember", getDatabase());
			intent.putExtra("lang", lang);
			
			startActivity(intent);
		}
	};
	
	public Bundle getAlllangs() {
		if (isRead()) {
			LangDAO lang = new LangDAO();
			Bundle resultVN = new Bundle();
			Bundle resultEN = new Bundle();
			Bundle resultKR = new Bundle();
			try {
				ArrayList<Languages> arr = lang.allLang();
				for (int i = 0; i < arr.size(); i++) {
						resultVN.putString(arr.get(i).getId(), arr.get(i).getVn());
						resultEN.putString(arr.get(i).getId(), arr.get(i).getEn());
						resultKR.putString(arr.get(i).getId(), arr.get(i).getKr());
					};

			} catch (Exception E) {
				E.printStackTrace();
			}
			alllangs.putBundle("VN", resultVN);
			alllangs.putBundle("EN", resultEN);
			alllangs.putBundle("KR", resultKR);
		}
		setRead(false);
		return alllangs;
	}
	public boolean isRead() {
		return read;
	}
	public void setRead(boolean read) {
		this.read = read;
	}
	
//	public void initAttribute(){
//		Bundle bundle = this.getAlllangs().getBundle(lang);
//		TextView titlePr = (TextView)findViewById(R.id.titleProjectManager);
//		titlePr.setText(bundle.getString("ac_SUProjectList") + "-" + bundle.getString("ac_Manager"));
//	}
	
}
